package com.htx.oldbookpavilion.web.mycenter;

import com.htx.oldbookpavilion.entity.BookInfo;
import com.htx.oldbookpavilion.entity.BookTypeDict;
import com.htx.oldbookpavilion.entity.UserInfo;
import com.htx.oldbookpavilion.service.mycenter.MyCenterService;
import com.htx.oldbookpavilion.common.StandardPageData;
import com.htx.oldbookpavilion.common.StandardResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/manager")
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    /**
     * 上传书籍信息
     * @param bookInfo
     * @return
     */
    @RequestMapping("/uploadBook")
    public StandardResponse uploadBook(@RequestBody BookInfo bookInfo) {
        StandardResponse<BookInfo> sResponse = new StandardResponse<>();
        BookInfo rBookInfo = myCenterService.uploadBook(bookInfo);
        if (rBookInfo != null) {
            sResponse.setSuccess(true);
            sResponse.setMsg("上传成功");
            sResponse.setData(rBookInfo);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("上传失败");
        }
        return sResponse;
    }

    /**
     * 更新书籍信息
     * @param bookInfo
     * @return
     */
    @RequestMapping("/updateUploadedBook")
    public StandardResponse updateUploadedBook(@RequestBody BookInfo bookInfo, HttpServletRequest request) {
        StandardResponse<BookInfo> sResponse = new StandardResponse<>();
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
        bookInfo.setLastModifier(userInfo.getUserName());
        bookInfo.setLastModifiedTime(new Date());
        BookInfo rBookInfo = myCenterService.updateBook(bookInfo);
        if (rBookInfo != null) {
            sResponse.setSuccess(true);
            sResponse.setMsg("更新成功");
            sResponse.setData(rBookInfo);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("更新失败");
            sResponse.setData(null);
        }
        return sResponse;
    }


    /**
     * 分页获取已上传书籍
     * @param sPageData
     * @return
     */
    @RequestMapping("/getUploadedBooksByPage")
    public StandardResponse getUploadedBooksByPage(@RequestBody StandardPageData<BookInfo> sPageData){
        StandardResponse<StandardPageData<BookInfo>> sResponse = new StandardResponse<>();
        sPageData.setStartIndex(sPageData.calStartIndex());
        sPageData = myCenterService.getUploadBooks(sPageData);
        if (sPageData != null && sPageData.getRecords() != null) {
            sResponse.setSuccess(true);
            sResponse.setMsg("获取已上传书籍成功");
            sResponse.setData(sPageData);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("获取已上传书籍失败");
        }
        return sResponse;
    }

    /**
     * 删除已上传书籍
     * @param bookInfoList
     * @return
     */
    @RequestMapping("/deleteUploadedBooks")
    public StandardResponse deleteUploadedBooks(@RequestBody List<BookInfo> bookInfoList) {
        StandardResponse<List<BookInfo>> sResponse = new StandardResponse<>();
        if (bookInfoList == null || bookInfoList.size() == 0) {
            sResponse.setSuccess(false);
            sResponse.setMsg("待删除书籍数为0，删除失败");
            sResponse.setData(null);
            return sResponse;
        }
        int count = myCenterService.deleteUploadBooks(bookInfoList);
        if(count > 0){
            sResponse.setSuccess(true);
            sResponse.setMsg("删除成功");
            sResponse.setData(bookInfoList);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("删除失败");
            sResponse.setData(null);
        }
        return sResponse;
    }


    /**
     * 校验密码
     * @param userInfo
     * @return
     */
    @RequestMapping("/validatePassword")
    public StandardResponse validatePassword(@RequestBody UserInfo userInfo){
        StandardResponse<Boolean> sResponse = new StandardResponse<>();
        boolean isValidate = myCenterService.validatePassword(userInfo);
        if(isValidate){
            sResponse.setSuccess(true);
            sResponse.setMsg("确认密码成功");
            sResponse.setData(true);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("确认密码失败");
            sResponse.setData(false);
        }
        return sResponse;
    }

    /**
     * 根据用户id查询用户信息。
     * @param userInfoIds
     * @return
     */
    @RequestMapping("/getUserInfoByIds")
    public StandardResponse getUserInfoByIds(String userInfoIds){
        StandardResponse<List<UserInfo>> sResponse = new StandardResponse<>();
        //分割用户ID
        String[] ids = userInfoIds.split(",");
        List<UserInfo> userInfoList = myCenterService.getUserInfoByIds(ids);
        if(userInfoList == null || userInfoList.size() == 0){
            sResponse.setSuccess(false);
            sResponse.setMsg("查询失败");
            sResponse.setData(null);
        }else{
            sResponse.setSuccess(true);
            sResponse.setMsg("查询成功");
            sResponse.setData(userInfoList);
        }
        return sResponse;
    }
}
